package com.lzxuni.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lzxuni.po.*;
import com.lzxuni.service.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.lzxuni.utils.JsonResultUtil;
import com.lzxuni.utils.MethodUtil;
import com.lzxuni.utils.UrlUtil;
import com.lzxuni.utils.UuidUtil;


@Controller
@RequestMapping("/")
public class QxDataController {
    @Autowired
    private QxDataService qxDataService;
    @Autowired
    private RhDataService rhDataService;
    @Autowired
    private YongHuService yongHuService;

    @Autowired
    private ForwardService forwardService;
    @Autowired
    private ErrorDataService errorDataService;

    /**
     * @描述 气象数据接口
     * @参数 data 数据 userkey设备号 date时间
     * @返回值
     * @创建人 gyl
     * @创建时间 2018/9/4
     * @修改人和其它信息
     */
    @ResponseBody
    @RequestMapping("shuju.html")
    public void shuju(String data, String userkey, String date) throws Exception {
        //System.out.println("i come !!!!!!");
        qxDataService.insertQxData(data, userkey, date);
    }

    @RequestMapping("list.html")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView("/area/list");
        return mv;
    }



    @RequestMapping("/listDo.html")
    public void listDo(QxDataCostom qxDataCustom, HttpServletRequest request, HttpServletResponse response) throws Exception {

        int pageNumber = request.getParameter("pageNumber") == null ? 1 : Integer.parseInt(request.getParameter("pageNumber"));
        int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request
                .getParameter("pageSize"));

        // 分页
        @SuppressWarnings("rawtypes")
        PageInfo pageInfo = qxDataService.findQxGroupList(pageNumber, pageSize, qxDataCustom);

        @SuppressWarnings("unchecked")
        List<QxData> qxDataList = pageInfo.getList();
        // 返回json数组分页
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();

        for (Iterator<QxData> iterator = qxDataList.iterator(); iterator.hasNext(); ) {
            JSONObject jsonObject1 = new JSONObject();
            QxData qxData = iterator.next();
            //检查是否为userkey创建名称
            YongHu yongHu = yongHuService.findYongHuById(qxData.getUserkey());
            if (yongHu == null) {
                YongHu yongHu2 = new YongHu();
                yongHu2.setUserkey_id(qxData.getUserkey());
                yongHu2.setCreateTime(MethodUtil.getDate(1, null));
                yongHuService.insertYongHu(yongHu2);
                yongHu = yongHuService.findYongHuById(qxData.getUserkey());
            }
            jsonObject1.put("userkey", qxData.getUserkey());
            jsonObject1.put("userName", yongHu.getUserName());
            jsonObject1.put("createTime", MethodUtil.formatDate(3, qxData.getCreateTime()));
            array.add(jsonObject1);
        }
        jsonObject.put("total", pageInfo.getTotal());
        jsonObject.put("rows", array);

        // 输出
        MethodUtil.writer(response, jsonObject.toString());


    }




    // 部门添加展示页面
    @RequestMapping("checkall.html")
    public ModelAndView check() throws Exception {
        ModelAndView mv = new ModelAndView("/area/checkall");
        return mv;

    }

    @RequestMapping("/checkallDo.html")
    public void checkallDo(QxDataCostom qxDataCustom, HttpServletRequest request, HttpServletResponse response) throws Exception {

        int pageNumber = request.getParameter("pageNumber") == null ? 1 : Integer.parseInt(request.getParameter("pageNumber"));
        int pageSize = request.getParameter("pageSize") == null ? 10 : Integer.parseInt(request
                .getParameter("pageSize"));

        // 分页
        @SuppressWarnings("rawtypes")
        PageInfo pageInfo = qxDataService.findQxDataList(pageNumber, pageSize, qxDataCustom);
        @SuppressWarnings("unchecked")
        List<QxData> qxDataList = pageInfo.getList();
        // 返回json数组分页
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();

        for (Iterator<QxData> iterator = qxDataList.iterator(); iterator.hasNext(); ) {
            JSONObject jsonObject1 = new JSONObject();
            QxData qxData = iterator.next();

            jsonObject1.put("id", qxData.getId());
            jsonObject1.put("userkey", qxData.getUserkey());
            jsonObject1.put("createTime", MethodUtil.formatDate(0, qxData.getCreateTime()));
            jsonObject1.put("kqwd", qxData.getKqwd());
            jsonObject1.put("kqsd", qxData.getKqsd());
            jsonObject1.put("gz", qxData.getGz());
            jsonObject1.put("trwd", qxData.getTrwd());
            jsonObject1.put("trsd", qxData.getTrsd());
            jsonObject1.put("fs", qxData.getFs());
            jsonObject1.put("fx", qxData.getFx());
            jsonObject1.put("eyht", qxData.getEyht());
            jsonObject1.put("yl", qxData.getYl());

            array.add(jsonObject1);
        }
        jsonObject.put("total", pageInfo.getTotal());
        jsonObject.put("rows", array);

        // 输出
        MethodUtil.writer(response, jsonObject.toString());


    }

    @RequestMapping("insert.html")
    public ModelAndView insert() throws Exception {


        ModelAndView mv = new ModelAndView("/area/insert");

        return mv;
    }


    @RequestMapping("update.html")
    public ModelAndView update(String userkey) throws Exception {
        YongHu yongHu = yongHuService.findYongHuById(userkey);
        ModelAndView mv = new ModelAndView("/area/update");
        mv.addObject("yongHu", yongHu);
        return mv;
    }

    @ResponseBody
    @RequestMapping("updateDo.html")
    public Object updateDo(YongHu yongHu) throws Exception {
        yongHuService.updateYongHu(yongHu);
        return JsonResultUtil.getSuccessJson("成功！");
    }


}